<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
</head>
<body class="layui-layout-body">
<input id="rMsg" th:value="${rMsg}" type="hidden"/>
<div class="layui-container" style="margin-top: 50px;width: 1000px;">

    <table class="layui-hide" id="test" style="width: 300px" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>
    <script>
        layui.use(['table','form','element'], function () {
            var table = layui.table;

            table.render({
                elem: '#test'
                , url: '/user/findAll'
                , title: '用户列表'
                , cols: [
                    [
                        {field: 'id', width: 70, title: '编号', sort: true}
                        , {field: 'userName', width: 100, title: '用户名'}
                        , {field: 'nickName', width: 100, title: '昵称'}
                        , {field: 'gender', width: 70, title: '性别'}
                        , {field: 'telephone', width: 130, title: '联系电话'}
                        , {field: 'registerDate', width: 180, title: '注册日期'}
                        , {field: 'address', width: 250, title: '地址'}
                        , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 120}
                    ]
                ]
                , page: true
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                console.log(obj.data);
                if (obj.event === 'del') {

                    layer.confirm('确定要删除吗？', function (index) {
                        window.location.href = "/user/deleteById/" + data.id;
                        layer.close(index);
                    });
                }else if (obj.event === 'edit') {
                    window.location.href = "/user/findById2/" + data.id;
                }
            });

            var rMsg=$('#rMsg').val();
            if(rMsg!=null&&rMsg!==''){
                if(rMsg==='error'){
                    layer.msg('用户已存在!请重新添加!');
                }else if(rMsg==='success'){
                    layer.msg('添加成功！', {
                        icon: 1,
                        time: 3000
                    });
                }
            }
        });


    </script>

</div>
<script>
    //二级菜单联动
    layui.use('element', function () {
        var element = layui.element;

    });
</script>
</body>
</html>